import {URI_GETSMALLHOUSELIST,URI_GETKEYWORDS} from "../../api";
import {ajax} from "../../utils/httpRequest";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    inputVal: '',
    selectList: ['二手房', '租房', '新房'],
    selectedIndex: 0,
    hidden: true,
    houseList: [],
    hots_list: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getSmallHouseList();
    this.getkeywords()
  },
  getkeywords(){
    ajax.get(URI_GETKEYWORDS,{},{},res=>{
      this.setData({
        hots_list:res.data
      })
    });
  },
  getSmallHouseList() {
    ajax.get(URI_GETSMALLHOUSELIST, {
      type: 1
    }, {}, res => {
      let arr = res.data;
      let newArr = arr.splice(arr.length - 5, 5);
      this.setData({
        houseList: newArr
      })
    })
  },
  toggleShowSelect() {
    this.setData({
      hidden: !this.data.hidden
    })
  },
  selectClick(e) {
    // e.target.
    let index = e.currentTarget.dataset.index;
    this.setData({
      selectedIndex: index,
      hidden: true
    });
    if (index == 0) {
      this.setTitle('搜索二手房');
    } else if (index == 1) {
      this.setTitle('搜索租房');
    } else if (index == 2) {
      this.setTitle('搜索新房');
    }
  },

  //input 事件

  watchInput(e) {
    this.setData({
      inputVal: e.detail.value
    });
  },
  hotsClick(e){
    let value = e.currentTarget.dataset.value;
    wx.navigateTo({
      url:`/pages/searchResult/searchResult?key=${value}&label=${this.data.selectedIndex}`
    })
  },
  inputConfirm(){
    if(this.data.inputVal){
      wx.navigateTo({
        url:`/pages/searchResult/searchResult?key=${this.data.inputVal}&label=${this.data.selectedIndex}`
      })
    }else {
      wx.showModal({
        title:'温馨提示',
        content:'请输入小区关键字',
        showCancel:false,
        confirmColor:'#ff474f'
      })
    }
  },
  clearInputVal() {
    this.setData({
      inputVal: ''
    })
  },
  setTitle(title) {
    wx.setNavigationBarTitle({
      title: title
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})